package com.ali.ding;

import android.content.Context;

import androidx.annotation.NonNull;

import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** DingRtcPlugin */
public class DingRtcPlugin implements FlutterPlugin, MethodCallHandler {

  private MethodChannel channel;
  private Context context;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    context = flutterPluginBinding.getApplicationContext();
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "ding_rtc_engine");
    channel.setMethodCallHandler(this);

    flutterPluginBinding.getPlatformViewRegistry().registerViewFactory(
            "DingRtcVideoView",
            new DingRtcViewFactory(flutterPluginBinding.getBinaryMessenger()));
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method) {
      case "initEngine":
        String extra = (String) call.arguments;
        DingRtcManager.getInstance().init(context, this, extra);
        result.success(null);
        break;
      case "destroyEngine":
        DingRtcManager.getInstance().destroy();
        result.success(null);
        break;
      case "enterRoom":
        DingRtcManager.getInstance().enterRoom((Map) call.arguments);
        result.success(null);
        break;
      case "exitRoom":
        DingRtcManager.getInstance().leaveRoom();
        result.success(null);
        break;
      case "startLocalPreview":{
        boolean front = call.argument("front");
        int viewId = call.argument("viewId");
        DingRtcManager.getInstance().startLocalPreview(front, viewId);
        result.success(null);
      }break;
      case "startRemoteView": {
        String userId = call.argument("userId");
        int viewId = call.argument("viewId");
        DingRtcManager.getInstance().startRemoteView(userId, viewId);
        result.success(null);
      }break;
      case "muteLocalAudio":
        DingRtcManager.getInstance().muteLocalAudio((Boolean) call.arguments);
        result.success(null);
        break;
      case "enableLocalVideo":
        DingRtcManager.getInstance().enableLocalVideo((Boolean) call.arguments);
        result.success(null);
        break;
      case "enableSpeakerphone":
        DingRtcManager.getInstance().enableSpeakerphone((Boolean) call.arguments);
        result.success(null);
        break;
      case "switchCamera":
        DingRtcManager.getInstance().switchCamera();
        result.success(null);
        break;
      case "setLogLevel":
        DingRtcManager.getInstance().setLogLevel((Integer) call.arguments);
        result.success(null);
        break;
      case "snapshotVideo":
        String userId = call.argument("userId");
        String path = call.argument("path");
        boolean isScreen = call.argument("isScreen");
        result.success(DingRtcManager.getInstance().snapshotVideo(userId, isScreen, path));
        break;
      default:
        result.notImplemented();
        break;
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    context = null;
    channel.setMethodCallHandler(null);
    channel = null;
    DingRtcManager.getInstance().destroy();
  }

  public void callBackToFlutter(String method, Object arguments) {
    if(channel != null) channel.invokeMethod(method, arguments);
  }
}
